apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

def getAvailableLocales() {
    def tree = fileTree(dir: 'src/main/res', include: '**/strings.xml')
    return tree.collect {
        def lang = it.getParentFile().getName() - "values-" - "values"

        // We want a name which would be understood by Locale::forLanguageTag, so we should do
        // do a simple conversion for an edge case:
        //   "pt-rBR" -> "pt-BR"
        //   "zh-rCN" -> "zh-CN"
        //   and so on
        lang = lang.replace("-r", "-")

        if (lang.empty) {
            lang = "en"
        }

        lang
    }
}

android {

    flavorDimensions "one"

    compileSdkVersion 29
    buildToolsVersion '29.0.3'

    lintOptions {
        abortOnError true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "net.programmierecke.radiodroid2"
        minSdkVersion 16
        targetSdkVersion 29

        versionCode 94
        versionName "0.84"

        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

        // Define your application name here.
        // It must neither be present in /res/values/strings.xml
        // nor in /res/values/string_no_translate.xml
        resValue 'string', 'app_name_untranslated', 'RadioDroid'

        // There is no easy way to get available locales during runtime, and especially before
        // tests setup.
        def escapedLocales = getAvailableLocales().collect { "\"" + it + "\"" }
        buildConfigField "String[]", "AVAILABLE_LOCALES", String.format("{ %s }", escapedLocales.join(","))

        testInstrumentationRunner "net.programmierecke.radiodroid2.tests.CustomTestRunner"

        // The following argument makes the Android Test Orchestrator run its
        // "pm clear" command after each test invocation. This command ensures
        // that the app's state is completely cleared between tests.
        testInstrumentationRunnerArguments clearPackageData: 'true'

        buildConfigField "java.util.concurrent.atomic.AtomicBoolean", "IS_TESTING", "new java.util.concurrent.atomic.AtomicBoolean(false)"

        multiDexKeepProguard file('multidex-config.pro')
    }

    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }

    sourceSets {
        // Store flags in subfolder to not pollute drawables
        // https://antonioleiva.com/android-multiple-resource-folders/
        main.res.srcDirs += ["src/main/res/drawable/flags"]

        androidTest {
            resources.srcDirs += ['src/androidTest/resources']
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

            lintOptions {
                disable 'MissingTranslation'
            }
        }
    }
    productFlavors {
        play {
            dimension "one"
        }
        free {
            dimension "one"
        }
    }

    testOptions {
        unitTests.all {
            useJUnit()
        }
        animationsDisabled = true
        unitTests.returnDefaultValues = true
    }
}

ext {
    exoplayerVersion = '2.11.7'
    iconicsVersion = '4.0.2'
}

dependencies {
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.material:material:1.3.0-alpha04'
    implementation 'androidx.preference:preference:1.1.1'
    implementation 'androidx.mediarouter:mediarouter:1.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.tvprovider:tvprovider:1.0.0'

    // Keep OkHttp 3.12.X to support Android 4.X, see https://developer.squareup.com/blog/okhttp-3-13-requires-android-5
    //noinspection GradleDependency
    implementation 'com.squareup.okhttp3:okhttp:3.12.12'

    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'jp.wasabeef:picasso-transformations:2.3.1'
    implementation "com.google.android.exoplayer:exoplayer-core:$exoplayerVersion"
    implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayerVersion"
    implementation "com.mikepenz:iconics-core:$iconicsVersion"
    implementation "com.mikepenz:iconics-views:$iconicsVersion"
    implementation 'com.mikepenz:google-material-typeface:3.0.1.4.original-kotlin@aar'
    implementation 'com.mikepenz:community-material-typeface:3.7.95.4-kotlin@aar'
    implementation 'com.github.rustamg:file-dialogs:1.0'
    implementation 'info.debatty:java-string-similarity:2.0.0'
    implementation 'me.xdrop:fuzzywuzzy:1.2.0'
    implementation 'com.github.zawadz88.materialpopupmenu:material-popup-menu:3.4.0'
    implementation 'com.github.ByteHamster:SearchPreference:v2.0.0'

    implementation 'androidx.room:room-runtime:2.2.5'
    annotationProcessor 'androidx.room:room-compiler:2.2.5'

    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"

    implementation 'androidx.paging:paging-runtime:2.1.2'

    playImplementation 'com.google.android.gms:play-services-cast:19.0.0'
    playImplementation 'com.google.android.gms:play-services-cast-framework:19.0.0'
    playImplementation 'com.google.android.gms:play-services-safetynet:17.0.0'

    testImplementation 'junit:junit:4.13.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.7.0'

    androidTestImplementation 'androidx.test:core:1.3.1-alpha02'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'
    // Keep this versions 1.3.0 until bumping it won't break building of AndroidTest
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test:rules:1.3.0'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'

    //noinspection GradleDependency
    androidTestImplementation("com.squareup.okhttp3:mockwebserver:3.12.6")
    androidTestImplementation("com.github.YarikSOffice:lingver:1.2.1") {
        exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk7'
    }

    androidTestUtil 'androidx.test:orchestrator:1.3.0'
}


// https://stackoverflow.com/questions/24649240/build-release-apk-with-customize-name-format-in-android-studio
// https://stackoverflow.com/questions/32092665/resolve-application-label-for-each-build-type/32220436#32220436
// https://stackoverflow.com/questions/18332474/how-to-set-versionname-in-apk-filename-using-gradle

// Gradle 3.0
// https://stackoverflow.com/questions/44800028/change-apk-filename-in-gradle-failed-with-gradle3-0-0-alpha4

android.applicationVariants.all { variant ->

    variant.outputs.all { output ->

        // get app_name field from defaultConfig
        def appName = variant.mergedFlavor.resValues.get('app_name_untranslated').getValue()
        appName = "${appName}"

        // concat new App name with each flavor's name

        variant.productFlavors.each { flavor ->
            appName += "-${flavor.name}"
        }

        // optionally add buildType name
        appName += "-${variant.buildType.name}"

        // your requirement: if buildType == debug, add DEV to App name
        if (variant.buildType.name == "debug") {
            appName += "-DEV"
        }

        // if you want, you can set a new resValue
        // variant.resValue 'string', 'app_name', appName

        /*
         * Gets the version name from the latest Git tag
         */

        def gitHash = { ->
            def stdout = new ByteArrayOutputStream()
            try {
                exec {
                    commandLine 'git', 'rev-parse', '--short', 'HEAD'
                    standardOutput = stdout
                }
                return stdout.toString().trim()
            } catch (Exception ignored) {
                return "No commit hash"
            }
        }

        resValue 'string', 'GIT_HASH', gitHash()

        def getBuildDate = {
            new Date().format('yyyy-MM-dd HH:mm:ss')
        }

        resValue 'string', 'BUILD_DATE', getBuildDate()

        def finalName = appName

        if (!versionName.isEmpty()) {
            finalName += "-" + versionName
        }

        finalName += "-" + gitHash() + ".apk"
        // outputFileName = "${variant.name}-${variant.versionName}.apk"
        outputFileName = finalName
    }
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        }
    }
}